﻿using ClassDAL.Entities;
using ClassDAL.Models;
using NHibernate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate.Criterion;

namespace ClassDAL
{
    public class ClassDAL : IClassDAL
    {
        private ISession Session; 

        public ClassDAL(ISession Session)
        {
            // TODO: Complete member initialization
            this.Session = Session;
        }
        public List<ClassModel> GetClass(ClassSearchCriteria classSearchCriteria)
        {
            var criteria = Session.CreateCriteria(typeof(ClassModel));

            if (classSearchCriteria != null)
            {
                // Search
                if (classSearchCriteria.Id > 0)
                {
                    criteria.Add(Expression.Eq("Id", classSearchCriteria.Id));
                }
                if (!string.IsNullOrEmpty(classSearchCriteria.Name))
                {
                    criteria.Add(Expression.Like("Name", classSearchCriteria.Name, MatchMode.Anywhere));
                }
                if (classSearchCriteria.ProjectId > 0)
                {
                    criteria.Add(Expression.Eq("ProjectId", classSearchCriteria.ProjectId));
                }
            }
            
            return criteria.List<ClassModel>().ToList();
        }
        public int AddClass(ClassModel @class)
        {
            Session.Save(@class);
            if (@class.Attributes != null)
            {
                for (int i = 0; i < @class.Attributes.Count; i++)
                {
                    Session.Save(@class.Attributes[i]);
                }
            }
            return @class.Id;
        }
    }
}
